/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao

import entity.Email;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
/**
 *
 * @author JAVIER
 */
class EmailsDAO  implements DAO <Email> {
    
    private Integer ID = 0;
            
    private static final String SQL_INSERT = " INSERT INTO Emails ( clientid, email, type) VALUES ( ?, ?, ?)";
    
    private static final String SQL_SELECT = "SELECT * FROM Emails where id= ?";
    
    private static final String SQL_SELECT_PROPERTY = " SELECT * FROM Emails where ? = ?" ;
    
    private static final String SQL_SELECT_All = " SELECT * FROM Emails";
    
    private static final String SQL_UPDATE = " UPDATE Emails SET id=?, clientid=?, email=?, type=? WHERE id = ?";

    private static final String SQL_DELETE = " DELETE FROM Emails WHERE id = ? ";
    
    Integer create(Email email, Connection connect) throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_INSERT,Statement.RETURN_GENERATED_KEYS);
      
            ps.setint(1, email.getClient().Id());
            ps.setString(2, email.getEmail());
            ps.setString(3,email.getType());

            ID = ps.executeUpdate();
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Boolean remove(Email email, Connection connect) throws SQLException {
        Boolean result=false;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_DELETE);
            ps.setInt(1, email.getId());
            ps.executeUpdate();
            result = true;
        } finally {
            close(ps);
        }
        return result;
        //TODO implement
    }
    
    Boolean update(Email email, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_UPDATE,Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, email.getId());
            ps.setInt(2, email.getClient().getId());
            ps.setString(3, email.getEmail());
            ps.setString(4,email.getType());
            ps.setInt(5, email.getId());
            ID = ps.executeUpdate();
            
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Email find(Integer id, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT);
            ps.setString(1, id);
            rs = ps.executeQuery();
            Email email = new Email();
            result = null;
            email = get_Email(rs);
            return email;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List findAll(Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Email email = new Email();
            result = null;
            while (rs.next()) {
                email = get_Email(rs);
                results.add(email);}
            return result;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List select_by_property(String camp, String Value, Connection connect)throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_PROPERTY);
            ps.setString(1, camp);
            ps.setString(2, value);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Email email = new Email();
            result = null;
            while (rs.next()) {
                email = get_Email(rs);
                results.add(email);}
            return result;

        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
        
    Email get_Email (ResultSet rs) throws SQLException {
 
        Email email = new Email();
        email.setId(rs.getInt("id"));
        email.setClientId(rs.getInt("clientId"));
        email.setEmail(rs.getString("email"));
        email.setType(rs.getString("type"));

        return email;
    }
    
}



